import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:common_lib/common/input.dart';
import 'package:common_lib/patient_management/add_patient/add_patient_bloc.dart';
import 'package:common_lib/patient_management/patient_list/patient_list_entity.dart'
    as PatientBen;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AddPatientPage extends BaseBlocPage<AddPatientBloc, _AddPatientState> {
  AddPatientPage(Parameters parameters) : super(parameters);

  @override
  _AddPatientState generateThemeState() {
    return _AddPatientState();
  }
}

class _AddPatientState extends BaseBlocPageState<AddPatientBloc> {
  String title = "";
  PatientBen.Datum patientInfo = PatientBen.Datum();
  bool isEdit = false;
  void initState() {
    super.initState();
    isEdit = parameters.getBool("isEdit");
    title = isEdit ? "编辑患者信息" : "添加患者";
    if (isEdit) {
      patientInfo = PatientBen.Datum.fromJson(parameters.getMap("item"));
    }
    bloc.initialDatas(patientInfo: patientInfo, isEdit: isEdit);
  }

  @override
  ignoreWindowWidget() => false;

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(title: Text(title), actions: <Widget>[
        FlatButton(
          child: Text(
            "完成",
            style: TextStyle(color: Colors.white),
          ),
          onPressed: () {
            bloc.savePatientInfo(context, isEdit);
          },
        )
      ]),
      backgroundColor: theme.themeColor.themeBgColor,
      body: Container(
        child: SafeArea(
          child: Column(
            children: [
              Divider(
                  height: 8.0,
                  thickness: 8.0,
                  color: theme.themeColor.themeBgColor),
              Expanded(
                child: SingleChildScrollView(
                  // padding: EdgeInsets.only(left: 16, right: 16),
                  child: StreamBuilder(
                    stream: bloc.dataSourceSubject,
                    initialData: patientInfo,
                    builder: (context, snapshot) {
                      return Column(
                        children: [
                          _editPatientNameBuilder(snapshot.data.patientName),
                          Divider(height: 1),
                          _selectedGenderBuilder(snapshot.data.sex),
                          Divider(height: 1),
                          _editPhoneBuilder(snapshot.data.phone),
                          Divider(height: 1),
                          _selectedIdCardBuilder(snapshot.data.patientIdNumber),
                          Divider(height: 1),
                          _selectedDateBuilder(context, snapshot.data.birthDay),
                          Divider(height: 1),
                          _addressInfoBuilder(context, bloc.spliceAddress(),
                              snapshot.data.address),
                          _sicknessBilder(snapshot.data.sex)
                        ],
                      );
                    },
                  ),
                ),
              ),
              isEdit
                  ? SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: FlatButton(
                        color: Colors.white,
                        child: Text("删除此患者"),
                        textColor: Colors.red,
                        onPressed: () {
                          bloc.deletePatient(context);
                        },
                      ))
                  : Container()
            ],
          ),
        ),
      ),
    );
  }

  /// 编辑姓名
  Widget _editPatientNameBuilder(patientName) {
    return Container(
      child: _cellWidgetBuilder("姓名", true, true, onChanged: (value) {
        bloc.editPatientName(value);
      }, defaultValue: patientName, placeHolder: "输入患者姓名"),
    );
  }

  /// 选择性别
  Widget _selectedGenderBuilder(int sex) {
    return Container(
        child: _cellWidgetBuilder("性别", true, false, onTap: () {
      bloc.selectedGender(context);
    }, placeHolder: "选择患者性别", defaultValue: Utils.returnGenderByInt(sex)));
  }

  /// 编辑电话号码
  Widget _editPhoneBuilder(String phone) {
    return Container(
      child: _cellWidgetBuilder("联系电话", false, true, onChanged: (value) {
        bloc.editPhone(value);
      },
          keyboardType: TextInputType.phone,
          defaultValue: phone,
          maxLength: 11,
          placeHolder: "输入联系电话"),
    );
  }

  /// 选择身份证
  Widget _selectedIdCardBuilder(String idCard) {
    return Container(
      child: _cellWidgetBuilder("身份证号", false, true, onChanged: (value) {
        bloc.editIdCard(value);
      },
          keyboardType: TextInputType.text,
          defaultValue: idCard,
          placeHolder: "输入身份证号"),
    );
  }

  /// 选择年月日
  Widget _selectedDateBuilder(BuildContext context, String birthDay) {
    return Container(
        child: _cellWidgetBuilder("出生日期", true, false, onTap: () {
      bloc.selectDate(context, birthDay);
    }, placeHolder: "年-月-日", defaultValue: birthDay));
  }

  /// 添加地址
  Widget _addressInfoBuilder(
      BuildContext context, String address, String detailAddress) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          _cellWidgetBuilder("地址", true, false, onTap: () {
            bloc.selectAddress(context);
          }, placeHolder: "省-市-区", defaultValue: address),
          Padding(
              padding: EdgeInsets.only(left: 16, right: 16, bottom: 16),
              child: textAreaWidget(
                  placeholder: "详细地址：街道、小区、门牌号等",
                  onChanged: (value) {
                    bloc.editDetailAddress(value);
                  },
                  defaultValue: detailAddress))
        ],
      ),
    );
  }

  /// 添加病例
  Widget _sicknessBilder(sex) {
    return Container(
      color: Colors.white,
      child: SicknessInfoWidget((result) {
        bloc.editSickness(result);
      }, defValue: bloc.buildSickness(), isFemale: sex == 2),
    );
  }

  ///
  ///@title String 左边标题
  ///@isMust bool 是否必填
  ///@placeHolder String 占位
  ///@isInput bool 右边是否是输入框
  ///@onTap VoidCallback 右边控件点击事件
  ///@onChanged ValueChanged<String> 右边输入框事件
  ///@defaultValue String 右边默认值
  ///@keyboardType TextInputType 输入框类型
  ///@maxLength int 输入框最大输入长度
  Widget _cellWidgetBuilder(String title, bool isMust, bool isInput,
      {String placeHolder = "请输入...",
      VoidCallback onTap,
      ValueChanged<String> onChanged,
      String defaultValue,
      TextInputType keyboardType,
      int maxLength}) {
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16),
      color: Colors.white,
      height: 50,
      child: Row(
        children: [
          Offstage(
            offstage: !isMust,
            child: Text(
              "*",
              style: TextStyle(
                  color: Colors.red, fontWeight: FontWeight.w400, fontSize: 15),
            ),
          ),
          Text(
            title,
            style: TextStyle(
                color: ColorUtil.color("#787878"),
                fontWeight: FontWeight.w400,
                fontSize: 15),
          ),
          Spacer(),
          Expanded(
              child: Offstage(
            offstage: placeHolder == null,
            child: isInput
                ? TextField(
                    controller: TextEditingController(text: defaultValue),
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: placeHolder,
                        counterText: ""),
                    onChanged: onChanged,
                    keyboardType: keyboardType,
                    textAlign: TextAlign.right,
                    maxLength: maxLength,
                    maxLengthEnforced: true,
                    strutStyle: StrutStyle(fontSize: 15, height: 1),
                    style: TextStyle(color: Colors.black, fontSize: 15))
                : GestureDetector(
                    onTap: onTap,
                    child: ObjectUtil.isEmpty(defaultValue)
                        ? Text(placeHolder,
                            textAlign: TextAlign.right,
                            style: TextStyle(
                                color: ColorUtil.color("#787878"),
                                fontWeight: FontWeight.w400,
                                fontSize: 15))
                        : Text(
                            defaultValue,
                            textAlign: TextAlign.right,
                          ),
                  ),
          ))
        ],
      ),
    );
  }

  @override
  AddPatientBloc generateBloc() {
    return AddPatientBloc();
  }
}

class Int {}
